#-------------------------------------------------------------------------------
# Name:        Urho3DConvertAPItoHeader
# Purpose:
#
# Author:      trq
#
# Created:     28.10.2013
# Copyright:   (c) trq 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------

#Run it from directory where SpriptAPI.dox is placed

import re


def main():
    f1 = open('ScriptAPI.dox', 'r')
    f2 = open('ScriptAPI.h', 'w')

    classOrEnum = "enum"
    bracesBegin = 0
    defines = """#define import extern
#define private
#define interface class\n"""

    f2.write(defines)
    for line in f1:
        line = re.sub (r'^-\s', '', line)         #remove "-"
        if not "###" in line:
           line = re.sub (r'\n', ';\n', line)     #add ";"
        line = re.sub(r'\(readonly\)', '', line)  #remove "(readonly)"
        if  "/**" in line or "*/" in line:
            line = ""

        if classOrEnum == "enum" and bracesBegin: #check if it's time to add "}" before enums
           classOrEnum = "};\nenum"

        if "ScriptAPI_Classes Classes" in line:
           classOrEnum = "};\nclass"

        if "namespace" in line:                   #remove "namespace Urho3D"
           line = ""

        if "{" in line and not bracesBegin:
           line = ""                              #remove very first curly brace

        if "###" in line:                         #replace "###" with enum or class declaration
           bracesBegin = 1                        #remember that we have to place matching brace after class\enum
           line = line.replace ('###', classOrEnum)
           line = line + "{\n"

        comments = re.search(r'\\page|\\section|^Properties:|^Methods:', line)  #um, comment comment
        if comments:
           line = "//" + line

        if "[]" in line:
           line = re.sub (r'(\w+|\w+@)(\[\])',r'array<\1>', line) #fix arrays T[] -> array<T>
           #print (line)
        if line != ";\n": #filter garbage
           f2.write(line)

    f1.close()
    f2.close()

if __name__ == '__main__':
    main()
